package model;

import design.patterns.observer.Subject;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * Created by nuno-rafael-figueiredo on 17-03-2014.
 */
public class Map<Key, Value> extends Subject implements java.util.Map<Key, Value> {
    private final java.util.Map<Key, Value> map = new LinkedHashMap<>();

    @Override
    public final int size() {
        return map.size();
    }

    @Override
    public final boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public final boolean containsKey(Object o) {
        return map.containsKey(o);
    }

    @Override
    public final boolean containsValue(Object o) {
        return map.containsValue(o);
    }

    @Override
    public final Value get(Object o) {
        return map.get(o);
    }

    @Override
    public final Value put(Key key, Value value) {
        Value put = map.put(key, value);

        if (put != null)
            setChangedAndNotifyObservers();

        return put;
    }

    @Override
    public final Value remove(Object o) {
        Value remove = map.remove(o);

        if (remove != null)
            setChangedAndNotifyObservers();

        return remove;
    }

    @Override
    public final void putAll(java.util.Map<? extends Key, ? extends Value> map) {
        this.map.putAll(map);
        setChangedAndNotifyObservers();
    }

    @Override
    public final void clear() {
        map.clear();
        setChangedAndNotifyObservers();
    }

    @Override
    public final Set<Key> keySet() {
        return map.keySet();
    }

    @Override
    public final Collection<Value> values() {
        return map.values();
    }

    @Override
    public final Set<Entry<Key, Value>> entrySet() {
        return map.entrySet();
    }

    public static <Key, Value extends IItem<Key>> Map<Key, Value> create() {
        return new Map<>();
    }
}
